package com.myacg.core.dao.utils;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 

/**
 * һ���򵥵Ľ�List������jsonArray����ʽ�洢���ļ��еĲ�����
 * 
 * @author guoshichang
 *
 */
public class JsonFileUtil {
	
   /**
    * ��List�洢��file����jsonList�ķ�ʽ
    * @param files
    * @param savePath
    * @return
    */
   public static boolean saveListToFile(List files,String savePath){
	   	String cont=JSONArray.fromObject(files).toString();
	   	boolean isSuccess=FileUtils.saveStringToFile(cont, savePath, "utf-8");
	   	if(isSuccess){
	   		System.out.println("backfiles is saveSuccese to : "+savePath);
	   	}else{
	   		System.err.println("backfiles is saveFeild");
	   	}
	   	
	   	return isSuccess;
   }
   /**
    * ��jsonList�ļ����ݶ�ȡ������bakFile
    * @param savePath
    * @return
    */
   public static  List  readListFromFile(Class<?> objClass,String filePath){
		   String readCont=FileUtils.readStringFromFile(filePath, "utf-8");
		   if(readCont!=null && !readCont.equals("")){
			   JSONArray jsonarr = JSONArray.fromObject(readCont);
			   List list=JSONArray.toList( jsonarr ,objClass);
			   return list;
		   }
	   return  new ArrayList();
   }
   
   /**
    * ��һ��json�����ļ����ݶ�ȡ������Object��
    * @param savePath
    * @return
    */
   public static  Object  readObjectFromFile(Class<?> objClass,String filePath){
		   String readCont=FileUtils.readStringFromFile(filePath, "utf-8");
		   if(readCont!=null && !readCont.equals("")){
			   return  JSONObject.toBean(JSONObject.fromObject(readCont), objClass);
		   }
	   return  null;
   }
   
   /**
    * ��List�洢��file����jsonList�ķ�ʽ
    * @param files
    * @param savePath
    * @return
    */
   public static boolean saveObjectToFile(Object object,String savePath){
	   	String cont=JSONObject.fromObject(object).toString();
	   	boolean isSuccess=FileUtils.saveStringToFile(cont, savePath, "utf-8");
	   	if(isSuccess){
	   		System.out.println("object is saveSuccese to : "+savePath);
	   	}else{
	   		System.err.println("object is saveFeild");
	   	}
	   	return isSuccess;
   }
	
}
